package com.sxz.sbs.module.performance.fragment;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseFragment;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.performance.PerformanceRankActivity;
import com.sxz.sbs.module.performance.adapter.FieldRankAdapter;
import com.sxz.sbs.module.performance.mode.FieldRankBean;
import com.sxz.sbs.module.performance.presenter.FieldRankPresenter;
import com.sxz.sbs.module.performance.view.FieldRankView;
import com.sxz.sbs.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 案场榜
 */
public class FieldRankFragment extends BaseFragment<FieldRankPresenter> implements FieldRankView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private FieldRankAdapter fieldRankAdapter;
    private List<FieldRankBean.ListBean> dataList = new ArrayList<>();
    private int currentPage = 1;
    boolean isList = true;
    private int selectIndex ;
    @Override
    protected void init() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getFieldRank();
            }
        });
        refreshLayout.setRefreshing(true);
        getFieldRank();
        fieldRankAdapter = new FieldRankAdapter(dataList);
        recyclerView.setAdapter(fieldRankAdapter);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (!DateUtils.isSameMonth(((PerformanceRankActivity)getActivity()).getCurrentDate())) {
                    ToastUtils.showShort("此月已经不可点赞哟");
                    return;
                }
                selectIndex = position;
                addPraise(dataList.get(position).getId());
            }

        });
        fieldRankAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getFieldRank();
            }
        },recyclerView);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                refreshLayout.setRefreshing(true);
                getFieldRank();
            }
        });

    }
    private void getFieldRank () {
        isList = true;
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-01 00:00:00");
        String begin = simpleDateFormat.format(((PerformanceRankActivity)getActivity()).getCurrentDate());
        String end = simpleDateFormat.format(DateUtils.nextMonthFirstDate(((PerformanceRankActivity)getActivity()).getCurrentDate()));
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("beginTime",begin);
        map.put("endTime",end);
        map.put("pageNumber",currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getFieldRank(map);
    }
    private void addPraise (int id) {
        isList = false;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("Parking_Id",id);
        mPresenter.addFieldPraise(map);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.common_list_layout;
    }

    @Override
    protected FieldRankPresenter createPresenter() {
        return new FieldRankPresenter();
    }
    public void update () {
        refreshLayout.setRefreshing(true);
        currentPage = 1;
        getFieldRank();
    }

    @Override
    public void onGetFieldRankSuccess(FieldRankBean data) {
        if (mIsLive) {
            refreshLayout.setRefreshing(false);
            if (data != null && data.getList().size() >0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll( data.getList());
                    fieldRankAdapter.notifyDataSetChanged();
                } else {
                    dataList.addAll( data.getList());
                    fieldRankAdapter.notifyDataSetChanged();
                }
                fieldRankAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    fieldRankAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        fieldRankAdapter.loadMoreEnd();
                    } else {
                        fieldRankAdapter.loadMoreEnd(true);
                    }

                }
            }
        }
    }

    @Override
    public void onAddFieldPraiseSuccess() {
        dataList.get(selectIndex).setIsParise(1);
        dataList.get(selectIndex).setPraiseNum( dataList.get(selectIndex).getPraiseNum()+1);
        fieldRankAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(int errorTag, String msg) {
        if (isList) {
            refreshLayout.setRefreshing(false);
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                } else {
                    stateLayout.showEmpty();
                }
            } else {
                fieldRankAdapter.loadMoreFail();
            }
        } else {
            super.showError(errorTag,msg);
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        if (isList) {
            refreshLayout.setRefreshing(false);
            fieldRankAdapter.loadMoreFail();
        }
    }
}
